﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Iguana.Core.HypterText
{
    internal static class UtilDocTypes
    {
        /// <summary>
        /// Returns a list of all Document Types defined in Iguana.Core.Hypertext.DocTypes
        /// Use this function when an element is supported in all Iguana supported DocTypes
        /// </summary>
        /// <returns></returns>
        internal static DocumentTypeCollection GetAllDocumentTypes()
        {
            DocumentTypeCollection _returnList = new DocumentTypeCollection();

            Type[] typeList = GetTypesInNamespace(Assembly.GetExecutingAssembly(), "Iguana.Core.HypterText.DocTypes");

            for (int x = 0; x < typeList.Length; x++)
            {
                _returnList.Add((IDocumentType)System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(typeList[x].FullName));
            }

            return _returnList;
        }

        /// <summary>
        /// Returns an HTML4Compliance object pre-instantiated with all the common STF document types.
        /// Use of this function is limited to attributes where the W3C has published an attribute is supported DTD STF.
        /// </summary>
        /// <returns></returns>
        internal static HTML4Compliance ReturnCommonSTF()
        {
            HTML4Compliance m_Return = new HTML4Compliance();
            m_Return.SupportedDocumentTypes.Add(new DocTypes.W3C401Strict());
            m_Return.SupportedDocumentTypes.Add(new DocTypes.W3C401Transitional());
            m_Return.SupportedDocumentTypes.Add(new DocTypes.W3C401Frameset());
            m_Return.SupportedDocumentTypes.Add(new DocTypes.W3CXHTML1Frameset());
            m_Return.SupportedDocumentTypes.Add(new DocTypes.W3CXHTML1Strict());
            m_Return.SupportedDocumentTypes.Add(new DocTypes.W3CXHTML1Transitional());
            return m_Return;
        }

        /// <summary>
        /// Returns all common STF XHTML DocTypes.
        /// </summary>
        /// <returns></returns>
        internal static HTML4Compliance ReturnXHTMLSTF()
        {
            HTML4Compliance m_Return = new HTML4Compliance();
            m_Return.SupportedDocumentTypes.Add(new DocTypes.W3CXHTML1Frameset());
            m_Return.SupportedDocumentTypes.Add(new DocTypes.W3CXHTML1Strict());
            m_Return.SupportedDocumentTypes.Add(new DocTypes.W3CXHTML1Transitional());
            return m_Return;
        }

        /// <summary>
        /// Returns all common STF HTML DocTypes.
        /// NOTE: This is NOT XHTML.
        /// </summary>
        /// <returns></returns>
        internal static HTML4Compliance ReturnHTMLSTF()
        {
            HTML4Compliance m_Return = new HTML4Compliance();
            m_Return.SupportedDocumentTypes.Add(new DocTypes.W3C401Strict());
            m_Return.SupportedDocumentTypes.Add(new DocTypes.W3C401Transitional());
            m_Return.SupportedDocumentTypes.Add(new DocTypes.W3C401Frameset());
            return m_Return;
        }

        internal static Type[] GetTypesInNamespace(Assembly assembly, string nameSpace)
        {
            return assembly.GetTypes().Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal)).ToArray();
        }
    }
}
